package com.btshop.goods.controller;
import com.btshop.goods.pojo.Brand;
import com.btshop.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: BrandController
 * @description: Brand 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "BrandController")
@Slf4j
@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 根据分类id查询品牌
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "Brand根据分类id查询品牌",notes = "根据分类id查询品牌",tags = {"BrandController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "categoryId", value = "分类id", required = true, dataType = "long")
    })
    @GetMapping(value = "/category/{categoryId}" )
    public Result<List<Brand>> findByCategoryId(@PathVariable("categoryId") Long categoryId){
        List<Brand> brands = brandService.findByCategoryId(categoryId);
        return Result.buildSuccess("根据分类id查询品牌成功！",brands);
    }

    /***
     * Brand分页条件搜索实现
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Brand条件分页查询",notes = "分页条件查询Brand方法详情",tags = {"BrandController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Brand对象",value = "传入JSON数据",required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
        log.info("BrandController findPage params , brand : {} , page : {} , size : {}",brand,page,size);
        //调用BrandService实现分页条件查询Brand
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Brand分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Brand分页查询",notes = "分页查询Brand方法详情",tags = {"BrandController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("BrandController findPage params , page : {} , size : {}",page,size);
        //调用BrandService实现分页查询Brand
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param brand
     * @return
     */
    @ApiOperation(value = "Brand条件查询",notes = "条件查询Brand方法详情",tags = {"BrandController"})
    @PostMapping(value = "/search" )
    public Result<List<Brand>> findList(@RequestBody(required = false) @ApiParam(name = "Brand对象",value = "传入JSON数据",required = false) Brand brand){
        log.info("BrandController findList params , brand : {} ",brand);
        //调用BrandService实现条件查询Brand
        List<Brand> list = brandService.findList(brand);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Brand根据主键id删除",notes = "根据主键删除Brand方法详情",tags = {"BrandController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("BrandController delete params ,   id : {}   ",id);
        //调用BrandService实现根据主键删除
        brandService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Brand数据
     * @param brand
     * @param id
     * @return
     */
    @ApiOperation(value = "Brand根据id修改",notes = "根据ID修改Brand方法详情",tags = {"BrandController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Brand对象",value = "传入JSON数据",required = false) Brand brand,@PathVariable("id") Long id){
        log.info("BrandController update params ,  brand : {},  id : {}   ",brand,id);
        //设置主键值
        brand.setId(id);
        //调用BrandService实现修改Brand
        brandService.update(brand);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Brand数据
     * @param brand
     * @return
     */
    @ApiOperation(value = "Brand添加",notes = "添加Brand方法详情",tags = {"BrandController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Brand对象",value = "传入JSON数据",required = true) Brand brand){
        log.info("BrandController add params , brand : {}",brand);
        //调用BrandService实现添加Brand
        brandService.add(brand);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Brand数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Brand根据id查询",notes = "根据ID查询Brand方法详情",tags = {"BrandController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Long id){
        log.info("BrandController findById params ,   id : {}   ",id);
        //调用BrandService实现根据主键查询Brand
        Brand brand = brandService.findById(id);
        return Result.buildSuccess("查询成功！",brand);
    }

    /***
     * 查询Brand全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Brand",notes = "查询所Brand有方法详情",tags = {"BrandController"})
    @GetMapping
    public Result<List<Brand>> findAll(){
        //调用BrandService实现查询所有Brand
        List<Brand> list = brandService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
